package com.yl.ylnews.base.imp.menu.tab;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.yl.ylnews.NewsWebActivity;
import com.yl.ylnews.R;
import com.yl.ylnews.base.BaseMenuDetailPager;
import com.yl.ylnews.bean.TopNews;
import com.yl.ylnews.global.GlobalConstants;
import com.yl.ylnews.utils.CacheUtils;
import com.yl.ylnews.utils.PrefUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

import static android.content.ContentValues.TAG;

/**
 * Created by Administrator on 2016/10/20 0020.
 */

public class BaseTabPager extends BaseMenuDetailPager implements SwipeRefreshLayout.OnRefreshListener {
    private int position;
    private String mUrl;
    private ListView mListNews;
    private List<TopNews.ResultBean.DataBean> dataBeanList;
    private SwipeRefreshLayout srl_refresh;

    public BaseTabPager(Activity mActivity, int i) {
        super(mActivity);
        position = i;
    }

    @Override
    public View initView() {
        View view = View.inflate(mActivity, R.layout.pager_tab_detail, null);
        mListNews = (ListView) view.findViewById(R.id.lv_list_news);
        srl_refresh = (SwipeRefreshLayout) view.findViewById(R.id.srl_refresh);
        srl_refresh.setOnRefreshListener(this);
        return view;
    }

    @Override
    public void initData() {
        mUrl = GlobalConstants.SERVER_URL + GlobalConstants.NEWS_URl[position];
        Log.d(TAG, "initData: mUrl为: " + mUrl);
        String cache = CacheUtils.getCache(mUrl, mActivity);
        if (!TextUtils.isEmpty(cache)) {//有缓存,使用缓存
            Log.d(TAG, "initData: 是否复用缓存: " + !TextUtils.isEmpty(cache));
            processData(cache);
        }
        getDataFromServer();
        mListNews.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mActivity, NewsWebActivity.class);
                String url = dataBeanList.get(position).getUrl();
                intent.putExtra("start web view", url);
                mActivity.startActivity(intent);
            }
        });

    }

    private void processData(String response) {
        Gson gson = new Gson();
        TopNews topNews = gson.fromJson(response, TopNews.class);
        dataBeanList = topNews.getResult().getData();
        mListNews.setAdapter(new ListNewsAdapter());

    }

    private void getDataFromServer() {

        OkHttpUtils
                .get()
                .url(mUrl)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.d(TAG, "onResponse: 服务器返回的结果为: " + response);
                        PrefUtil.setSharedPerenceString(mActivity, "cache", response);
                        processData(response);
                        CacheUtils.setCache(mUrl, response, mActivity);
                        if(srl_refresh.isRefreshing()){
                            srl_refresh.setRefreshing(false);
                        }
                    }
                });
    }

    @Override
    public void onRefresh() {
        getDataFromServer();
    }

    class ListNewsAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            Log.d(TAG, "getCount: "+dataBeanList.size());
            return dataBeanList.size();

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TopNews.ResultBean.DataBean dataBean = dataBeanList.get(position);
            final ViewHolder viewHolder;
            if (convertView == null) {
                convertView = View.inflate(mActivity, R.layout.item_list_news, null);
               // convertView = LayoutInflater.from(mActivity).inflate(R.layout.item_list_news, mListNews);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            final String thumbnail_pic_s = dataBean.getThumbnail_pic_s();
            //new NetCacheUtils().getBitmapFromNet(viewHolder.ivIcon,thumbnail_pic_s);
            Glide.with(mActivity).load(thumbnail_pic_s).into(viewHolder.ivIcon);

            viewHolder.tvAuthorName.setText(dataBean.getAuthor_name());
            viewHolder.tvDate.setText(dataBean.getDate());
            viewHolder.tvNewsTittle.setText(dataBean.getTitle());
            return convertView;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        class ViewHolder {
            @BindView(R.id.iv_icon)
            ImageView ivIcon;
            @BindView(R.id.tv_author_name)
            TextView tvAuthorName;
            @BindView(R.id.tv_date)
            TextView tvDate;
            @BindView(R.id.tv_news_tittle)
            TextView tvNewsTittle;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }

}
